package cn.sfturing.service.impl;

import java.util.List;
import java.util.Map;

import cn.sfturing.dao.DoctorDao;
import cn.sfturing.dao.HospitalDao;
import cn.sfturing.entity.Doctor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.sfturing.dao.OfficeDao;
import cn.sfturing.entity.Hospital;
import cn.sfturing.entity.Office;
import cn.sfturing.service.OfficeService;

@Service
public class OfficeServiceImpl implements OfficeService {
	@Autowired
	private OfficeDao officeDao;
	@Autowired
	private DoctorDao doctorDao;
	@Autowired
	private HospitalDao hospitalDao;

	@Override
	public List<Office> findOfficeByHosName(String hospitalName) {

		return officeDao.findOfficeByHosName(hospitalName);
	}

	@Override
	public List<Office> findOfficeByRe(Map<String, Object> officeMap) {
		// TODO Auto-generated method stub
		return officeDao.findOfficeByRe(officeMap);
	}

	@Override
	public int findOfficeByReNum(List<Hospital> hospital) {
		// TODO Auto-generated method stub
		return officeDao.findOfficeByReNum(hospital);
	}

	@Override
	public Office findOfficeById(int id) {
		// TODO Auto-generated method stub
		return officeDao.findOfficeById(id);
	}

	@Override
	public List<Office> findOfficeByConditon(Office office, int start, int size) {
		// TODO Auto-generated method stub
		return officeDao.findOfficeByCondition(office.getHospitalName(), office.getOfficesName(), start, size);
	}

	@Override
	public int findOrderOfficeNum(Office office) {
		// TODO Auto-generated method stub
		return officeDao.findOrderOfficeNum(office.getHospitalName(), office.getOfficesName());
	}

	@Override
	public List<Office> findSuper(Office office) {
		return officeDao.findSuper(office);
	}

	@Override
	public Office findOfficeByHosNameAndOfficeName(String hospitalName, String officeName) {
		return officeDao.findOfficeByHosNameAndOfficeName(hospitalName,officeName);
	}

	@Override
	public boolean updateOfficeById(Office office) {
		return officeDao.updateOfficeById(office);
	}

	@Override
	public boolean delOfficeById(int Id) {
		boolean TF=false;
		Office office1 = officeDao.findOfficeById(Id);
		if(officeDao.delOfficeById(Id)>0){
			//科室删除成功,删除科室里所有医生
			List<Doctor> DoctorDelList = doctorDao.findAreaByHosAndOfficeName(office1.getHospitalName(), office1.getOfficesName());
			for (Doctor doc:DoctorDelList) {
				doctorDao.byIdDel(doc.getId());
			}
			//医院科室减1
				Hospital hospital = hospitalDao.findHosByName(office1.getHospitalName());
				if (null != hospital) {
					hospital.setHospitalOfficesNum(hospital.getHospitalOfficesNum()-1);
					hospitalDao.updataHospital(hospital);
				}
			TF=true;
		}
		return TF ;
	}

	@Override
	public List<Office> selectBK(Office office) {
		return officeDao.selectBK(office);
	}

	@Override
	public boolean insertOffice(Office office) {
		boolean tf=false;
		if(officeDao.insertOffice(office)>0){
			//科室添加成功，医院科室数加一
			Hospital hospital = hospitalDao.findHosByName(office.getHospitalName());
			if (null != hospital) {
				hospital.setHospitalOfficesNum(hospital.getHospitalOfficesNum()+1);
				hospitalDao.updataHospital(hospital);
			}
			tf=true;
		}
		return tf;
	}

	@Override
	public boolean updataOffice(Office office) {
		return officeDao.updataOffice(office)>0?true:false;
	}

}
